﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace exam
{
    public partial class EditerForm : Form
    {
        public EditerForm()
        {
            InitializeComponent();
        }
        private int Id;
        public EditerForm(int Id,string Name,int Age,int Score,string Sex,string Professional,string Class)
        {
            InitializeComponent();
            this.Id = Id;
            textBox1.Text = Name;
            textBox2.Text = Age.ToString();
            textBox3.Text = Score.ToString();
            textBox4.Text = Sex;
            textBox5.Text = Professional;
            textBox6.Text = Class;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var Name = textBox1.Text;
            var Age = textBox2.Text;
            var Score = textBox3.Text;
            var Sex = textBox4.Text;
            var Professional = textBox5.Text;
            var Class = textBox6.Text;

            //更新
            if (this.Id > 0)
            {
                var sql = string.Format("update Students set Name='{0}',Age='{1}',Score='{2}',Sex='{3}',Professional='{4}',Class='{5}' where Id='{6}'", Name, Age, Score, Sex, Professional, Class, this.Id);
                DBHelper.AddOrUpdateOrDelete(sql);
                MessageBox.Show("更新成功", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            }
            //添加
            else
            {
                var sql = string.Format("insert into Students (Name,Age,Score,Sex,Professional,Class) values ('{0}','{1}','{2}','{3}','{4}','{5}')", Name, Age, Score, Sex, Professional, Class);
                DBHelper.AddOrUpdateOrDelete(sql);
                MessageBox.Show("添加成功", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.Yes;
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
